Skip to content

Implement Decode, Encode and Type for Box, Arc, Cow and Rc #3674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

joeydewaal
Copy link
Contributor

fixes #3100

Implements Decode, Encode and Type for Box<T>, Arc<T>, Cow<'_,T> and Rc<T>. I left out the Decode impl for Rc<T> because of the Send trait bounds in QueryAs and QueryScalar (which makes it impossible to use Rc).

@joeydewaal joeydewaal changed the title Encode decode Implement Decode, Encode and Type for Box, Arc, Cow and Rc Jan 9, 2025
@abonander
Copy link
Collaborator

I left out the Decode impl for Rc<T> because of the Send trait bounds in QueryAs and QueryScalar (which makes it impossible to use Rc).

@joeydewaal technically it's still possible to use by getting the Row and then calling get(). Someone's gonna end up asking why it doesn't exist, and justifying that would be more work than just carrying the impl.

We can relax the Send bound on QueryAs/QueryScalar in the future when we get rid of BoxStream in the fetch() method:

) -> BoxStream<'e, Result<Either<DB::QueryResult, O>, Error>>

Copy link
Collaborator

@abonander abonander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous comment.

@joeydewaal
Copy link
Contributor Author

Makes sense, I added the impl for Rc

@abonander
Copy link
Collaborator

CI should be fixed if you rebase.

@joeydewaal joeydewaal force-pushed the encode-decode branch 9 times, most recently from b59de52 to 3857c69 Compare March 12, 2025 14:43
@joeydewaal joeydewaal requested a review from abonander March 19, 2025 13:02
@joeydewaal joeydewaal force-pushed the encode-decode branch 2 times, most recently from 608908c to c68cf4b Compare April 5, 2025 20:41
@joeydewaal joeydewaal force-pushed the encode-decode branch 3 times, most recently from eb5b015 to 2da2978 Compare April 5, 2025 21:49
@abonander abonander added this to the 0.9.0 milestone May 19, 2025
Copy link
Collaborator

@abonander abonander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wouldn't mind, let's just commit to making the Decode for Cow impl return Cow::Owned.

@joeydewaal
Copy link
Contributor Author

joeydewaal commented May 20, 2025

Right, that should be reflected in the latest changes.

Comment on lines +142 to +160
impl<'r, 'a, DB> Decode<'r, DB> for Cow<'a, str>
where
DB: Database,
String: Decode<'r, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
<String as Decode<DB>>::decode(value).map(Cow::Owned)
}
}

impl<'r, 'a, DB> Decode<'r, DB> for Cow<'a, [u8]>
where
DB: Database,
Vec<u8>: Decode<'r, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
<Vec<u8> as Decode<DB>>::decode(value).map(Cow::Owned)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these aren't necessary if you add T: ?Sized to the generic impl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

impl Decode, Encode and Type for smart pointer types like Cow
2 participants